package cn.thoughtworks.school.programCenter.util.excel;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class BasicQuizOverviewExcel extends Excel {

    public static Excel init(List<Map<String, String>> data) {
        Excel excel = new BasicQuizOverviewExcel();
        excel.data = data.stream()
            .sorted((a, b) -> Integer.parseInt(b.get("correctCount")) - Integer.parseInt(a.get("correctCount")))
            .collect(Collectors.toList());
        excel.workbook = new HSSFWorkbook();
        return excel;
    }

    @Override
    public HSSFWorkbook fillData() {
        HSSFSheet sheet = workbook.createSheet(getTitle());
        fillColumnsTitle(sheet, Arrays.asList("排名", "姓名","拼音", "Tag", "题目总数", "未完成数量", "正确个数", "错误个数", "正确率"));
        fillBody(sheet);
        return workbook;
    }

    private String getTitle() {
        return data.get(0).get("assignmentTitle");
    }


    protected void fillBody(HSSFSheet sheet) {
        for (int i = 0; i < data.size(); i++) {
            Map<String, String> studentStatus = data.get(i);
            HSSFRow row = createRow(sheet, i + 1);

            createCell(row, 0, i + 1 + "", getDefaultCellStyle());
            createCell(row, 1, studentStatus.get("name"), getDefaultCellStyle());
            createCell(row, 2, studentStatus.get("userName"), getDefaultCellStyle());
            createCell(row, 3, formatTag(studentStatus.get("tag")), getDefaultCellStyle());
            createCell(row, 4, studentStatus.get("total"), getDefaultCellStyle());
            createCell(row, 5, calculateUnFinishedNumber(studentStatus), getDefaultCellStyle());
            createCell(row, 6, studentStatus.get("correctCount"), getDefaultCellStyle());
            createCell(row, 7, studentStatus.get("wrongCount"), getDefaultCellStyle());
            createCell(row, 8, statisticCorrectProportion(studentStatus), getDefaultCellStyle());
        }
    }

    private String statisticCorrectProportion(Map<String, String> studentStatus) {
        Integer correctCount = Integer.valueOf(studentStatus.get("correctCount"));
        Integer wrongCount = Integer.valueOf(studentStatus.get("wrongCount"));
        Integer total = correctCount + wrongCount;
        if (total == 0) {
            return "0.00";
        }
        return new DecimalFormat("0.00").format((float)(correctCount) / total) + "";
    }

    private String calculateUnFinishedNumber(Map<String, String> studentStatus) {
        return Integer.valueOf(studentStatus.get("total"))
            - (Integer.valueOf(studentStatus.get("correctCount"))
            + Integer.valueOf(studentStatus.get("wrongCount"))) + "";
    }

    private String formatTag(String tag) {
        if (Objects.isNull(tag)) {
            return "";
        }
        return Arrays.asList(tag.split(","))
            .stream().map(item -> item.split("\\|")[0])
            .collect(Collectors.joining("、"));
    }

    protected void fillColumnsTitle(HSSFSheet sheet, List<String> titles) {
        HSSFRow row = sheet.createRow(0);
        row.setHeight((short) (25 * 25));
        for (int i = 0; i < titles.size(); i++) {
            createCell(row, i, titles.get(i), getDefaultCellStyle());
        }
    }
}
